package com.cmc.cloud.cmclink.doc.controller.admin.special;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.DgCommodityErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.DgCommodityPsnConvert;
import com.cmc.cloud.cmclink.doc.entity.DgCommodityPsnDO;
import com.cmc.cloud.cmclink.doc.service.DgCommodityPsnService;
import com.cmc.cloud.cmclink.doc.vo.special.dgcommoditypsnvo.DgCommodityPsnCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.dgcommoditypsnvo.DgCommodityPsnRespVO;
import com.cmc.cloud.cmclink.doc.vo.special.dgcommoditypsnvo.DgCommodityPsnUpdateReqVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 危险品品名子表库前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 危险品品名子表库")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/special/dg-commodity-psn")
public class DgCommodityPsnController {

    @Resource
    private DgCommodityPsnService dgCommodityPsnService;

    @Operation(summary = "创建危险品品名子表库")
    @PostMapping("/create")
    public CommonResult<Void> createDgCommodityPsn(@Valid @RequestBody DgCommodityPsnCreateReqVO createReqVO) {
        dgCommodityPsnService.createDgCommodityPsn(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新危险品品名子表库")
    @PostMapping("/update")
    public CommonResult<Void> updateDgCommodityPsn(@Valid @RequestBody DgCommodityPsnUpdateReqVO updateReqVO) {
        dgCommodityPsnService.updateDgCommodityPsn(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除危险品品名子表库")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> deleteDgCommodityPsn(@PathVariable("id") Long id) {
        dgCommodityPsnService.deleteDgCommodityPsn(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得危险品品名子表库")
    @GetMapping("/get/detail/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @UseResponsePropertyAdvice
    public CommonResult<DgCommodityPsnRespVO> getDgCommodityPsn(@PathVariable("id") Long id) {
        DgCommodityPsnDO dgCommodityPsn = dgCommodityPsnService.getDgCommodityPsn(id);
        if (dgCommodityPsn == null) {
            throw ServiceExceptionUtil.exception(DgCommodityErrorCodeConstants.DG_COMMODITY_PSN_NOT_EXISTED);
        }
        return CommonResult.success(DgCommodityPsnConvert.INSTANCE.convert(dgCommodityPsn));
    }

    @Operation(summary = "获得危险品品名子表库列表")
    @GetMapping("/get/{main_id}")
    @Parameter(name = "main_id", description = "品名主信息id", required = true)
    @UseResponsePropertyAdvice
    public CommonResult<List<DgCommodityPsnRespVO>> getDgCommodityPsnsByMainId(@PathVariable("main_id") Long mainId) {
        List<DgCommodityPsnDO> list = dgCommodityPsnService.getDgCommodityPsnsByMainId(mainId);
        return CommonResult.success(DgCommodityPsnConvert.INSTANCE.convertList(list));
    }

}
